#include <iostream>
#include <cstdio>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

const char* pathname = "/home/sxk/linux2/24_6_6";
int proj_id = 0x66;

int main()
{
    //获取key
    key_t key = ftok(pathname,proj_id);
    if(key < 0)
    {
        perror("ftok");
    }
    //获取共享内存
    int shmid = shmget(key,4096,IPC_CREAT);
    if(shmid < 0)
    {   
        perror("shmget");
    }
    //与共享内存关联指定shmid,不指定地址起始位置,读写权限
    char* msg = (char*)shmat(shmid,NULL,0);
    if(msg == (void*)-1)
    {  
        perror("shmat");
    }
    //开始发送消息
    char a = 'A';
    int i = 0;
    while(a < 'Z')
    {
        msg[i] = a + i;
        i++;
        sleep(1);
    }
    //发送完毕，去关联
    int t = shmdt(msg);
    if(t < 0 )
    {
        perror("shmdt");
    }
    return 0;
}